//
//  HJUserInfoController.m
//  KaiChat
//
//  Created by lx on 2023/10/20.
//

#import "HJUserInfoController.h"
#import "HJIM.h"
#import "HJChatController.h"

@interface HJUserInfoController ()

@property (nonatomic, weak) UILabel *remarksLabel;
@property (nonatomic, weak) UILabel *nicknameLabel;
@property (nonatomic, weak) UILabel *userIdLabel;
@property (nonatomic, weak) UILabel *signatureLabel;
@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) HJStyle2Button *sendMessageButton;
@property (nonatomic, weak) HJStyle1Button *applyAddButton;

@end

@implementation HJUserInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"Info";
    
    self.navigationBar.lineView.hidden = YES;
    
    UIView *headerView = [UIView addToView:self.view];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(102);
        make.top.offset(KNavBarHight);
    }];
    
    UIImageView *avatarImageView = [UIImageView addToView:headerView];
    avatarImageView.addCornerRadius(28);
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.width.height.offset(54);
        make.centerY.offset(0);
    }];
    //
    //    UILabel *remarksLabel = [UILabel lableWithSuperView:headerView font:UIFontMediumMake(16) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    //    [remarksLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(avatarImageView).offset(4);
    //        make.left.equalTo(avatarImageView.mas_right).offset(10);
    //    }];
    //
    UILabel *nicknameLabel = [UILabel lableWithSuperView:headerView font:UIFontMediumMake(16) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatarImageView).offset(4);
        make.left.equalTo(avatarImageView.mas_right).offset(10);
    }];
    
    UILabel *userIdLabel = [UILabel lableWithSuperView:headerView font:UIFontMake(12) color:UIColor.tj_868391Color title:nil textAlignment:NSTextAlignmentLeft];
    [userIdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nicknameLabel.mas_bottom).offset(6);
        make.left.equalTo(nicknameLabel);
    }];
    
    UIView *lineView = [UIView addToView:headerView];
    lineView.backgroundColor = UIColor.tj_F3F2F7Color;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.height.offset(1);
        make.bottom.offset(0);
    }];
    
    UILabel *signatureLabel = [UILabel lableWithSuperView:self.view font:UIFontMake(14) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    signatureLabel.numberOfLines = 0;
    [signatureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom).offset(12);
        make.left.right.inset(leftRightMargin);
    }];
    
    HJStyle2Button *sendMessageButton = [HJStyle2Button buttonWithSuperView:self.view font:UIFontMediumMake(14) color:nil title:@"Send message"];
    sendMessageButton.hidden = YES;
    [sendMessageButton addTarget:self action:@selector(didClickSendMessageButton) forControlEvents:UIControlEventTouchUpInside];
    [sendMessageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(20);
        make.height.offset(55);
        make.bottom.inset(10 + TabbarSafeBottomMargin);
    }];
    
    HJStyle1Button *applyAddButton = [HJStyle1Button buttonWithSuperView:self.view font:UIFontMediumMake(14) color:nil title:@"Add to Contacts"];
    applyAddButton.hidden = YES;
    [applyAddButton addTarget:self action:@selector(didClickApplyAddButton) forControlEvents:UIControlEventTouchUpInside];
    [applyAddButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.bottom.equalTo(sendMessageButton);
    }];
    
    _sendMessageButton = sendMessageButton;
    _applyAddButton = applyAddButton;
    
    _signatureLabel = signatureLabel;
    //    _remarksLabel = remarksLabel;
    _nicknameLabel = nicknameLabel;
    _userIdLabel = userIdLabel;
    _avatarImageView = avatarImageView;
    
    [self loadData];
}

- (void)didClickSendMessageButton {
    HJChatController *chatController = [HJChatController chatControllerWithSessionModel:[HJChatSessionModel modelWithBlock:^(HJChatSessionModel * _Nonnull model) {
        model.sessionId = toString(self.friendModel.sessionId);
        model.chatType = HJChatTypeSingle;
        model.uid = self.friendModel.userId;
        model.sessionName = self.friendModel.showName;
        model.receiveId = self.friendModel.uid;
    }]];
    [self.navigationController pushViewController:chatController];
}

- (void)didClickApplyAddButton {
    
    
}

- (void)loadData {
    
    if (_friendModel) {
        _sendMessageButton.hidden = NO;
        _applyAddButton.hidden = YES;
    }
    //现在直接查询本地的, 再请求网络接口
    [HJIM.shareIM.userManager loadUserInfoWithUserId:toString(_friendModel.userId) complete:^(HJChatUserModel * _Nullable model) {
        
        if (model.isFridend) {
            self.sendMessageButton.hidden = NO;
            self.applyAddButton.hidden = YES;
        } else if (model.relationship == -1 || model.relationship == 3) {
            self.sendMessageButton.hidden = YES;
            self.applyAddButton.hidden = NO;
        } else {
            self.sendMessageButton.hidden = YES;
            self.applyAddButton.hidden = YES;
        }
        
        [self.avatarImageView tj_setImageWithURL:model.avatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
        
        self.nicknameLabel.text = model.nickname;
        self.userIdLabel.text = model.uid;
        self.signatureLabel.attributedText = [model.brief titleMargin:3 alignment:NSTextAlignmentLeft];
    }];
}

@end
